// 传入node.uid,返回该节点的所有父节点uid
export function totalCount(key, tree) {
  let totalResult = [];
  // 通过uid从树找到节点,返回node --- obj
  function getNode(uid, tree) {
    let result = {};
    function loop(params, arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].uid == params) {
          result = arr[i];
          return "";
        }
        arr[i].child.length && loop(key, arr[i].child);
      }
    }
    loop(uid, tree);
    return result;
  }
  // 判断是否有父节点
  function getParent(key) {
    let node = getNode(key, tree)
    console.log("当前的值", node.uid, node.pUid != null);
    if (node.pUid != null) {
      totalResult.push(node.pUid);
      console.log("父节点", pUid);
        getParent(pUid);
    }
    return ;
  }
  getParent(key);
  console.log("totalReuslt", totalResult);
  return totalResult;
}

// 该节点是否有分支，
// target, tree

// 1.把所有的子节点返回
// 2.没有分支
export function getChildern(uidArr, tree) {
  const group = {};
  const result = [];
  // 获取该数组所有的子节点
  function getChildAll(node) {
    let group = [];
    function loop(node) {
      if (node.child) {
        node.child.forEach(item => {
          group.push(item.uid);
          loop(item);
        });
      }
    }
    loop(node);
    return group;
  }
  // 通过uid从树找到节点,返回node --- obj
  function getNode(uid, tree) {
    let result = {};
    function loop(params, arr) {
      arr.forEach(item => {
        item.uid == params && (result = item);
        item.child.lenght && loop(key, item.child);
      });
    }
    loop(uid, tree);
    return result;
  }
  uidArr.forEach(item => {
    group[item] = getChildAll(getNode(item, tree));
  });
  for (let i in group) {
    if (group[i].lenght) {
      group[i].forEach((g, index) => {
        uidArr.forEach(u => {
          u == g && group.splice(index, 1);
        });
      });
    }
    group[i].length == 0 && result.push(i);
  }
  return result;
}
